/*!
 * \file t_get_req_data.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"

#include "../../../../src/module/http/config.h"
#include <http/comm/http_comm.h>
#include <http/http_core.h>
#include <frct_comm.h>
#include "frreport_comm.h"
#include "../../../comm/apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    FILE *fp_file = NULL;
    char *p_tcp_body = NULL;
    int i_tcp_body_len = 149;
    int i_ret = 0;
    L7session_http_private *p_st_private = NULL;
    Http1_session *p_sess = NULL;
    Http_comm_session *p_comm = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_st_private = http_private_create();
    p_st_private->i_version = 1;
    p_st_private->p_http1_sess = http1_session_create();
    p_sess = p_st_private->p_http1_sess;
    p_comm = p_sess->p_st_comm_session;
    p_comm->i_data_len = 149;
    p_comm->i_flag_need = 2;

    i_ret = apmtest_create_ct(&p_st_conn, http_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 80);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    fp_file = fopen("t_comm/t_http_comm/get_req_data.txt","r");

    fseek(fp_file,0,SEEK_END);
    i_tcp_body_len = ftell(fp_file);

    p_tcp_body = (char *)frmalloc(i_tcp_body_len + 1);
    rewind(fp_file);
    fread(p_tcp_body,i_tcp_body_len,1,fp_file);
    fclose(fp_file);
    p_tcp_body[i_tcp_body_len] = '\0';


    i_ret = get_req_data(p_st_private, p_st_body_data, p_tcp_body, i_tcp_body_len);

    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_SUCC);


    http_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_tcp_body);
    apmtest_destroy_ct(&p_st_conn);

    return;
}


static void __case2(void)
{
    FILE *fp_file = NULL;
    char *p_tcp_body = NULL;
    int i_tcp_body_len = 149;
    int i_ret = 0;
    L7session_http_private *p_st_private = NULL;
    Http1_session *p_sess = NULL;
    Http_comm_session *p_comm = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_st_private = http_private_create();
    p_st_private->i_version = 1;
    p_st_private->p_http1_sess = http1_session_create();
    p_sess = p_st_private->p_http1_sess;
    p_comm = p_sess->p_st_comm_session;
    p_comm->i_data_len = 189;
    p_comm->i_flag_need = 2;

    i_ret = apmtest_create_ct(&p_st_conn, http_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 80);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    fp_file = fopen("t_comm/t_http_comm/get_req_data.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_tcp_body_len = ftell(fp_file);

    p_tcp_body = (char *)frmalloc(i_tcp_body_len + 1);
    rewind(fp_file);
    fread(p_tcp_body,i_tcp_body_len,1,fp_file);
    fclose(fp_file);
    p_tcp_body[i_tcp_body_len] = '\0';


    i_ret = get_req_data(p_st_private, p_st_body_data, p_tcp_body, i_tcp_body_len);

    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_TODO);


    http_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_tcp_body);
    apmtest_destroy_ct(&p_st_conn);

    return;
}



int add_suite_get_req_data(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" get_req_data()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);

    return FR_SUCC;
}
